// pages/school/school.js
import { school } from "../../server/school";
import { file } from "../../server/file";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        provinces: [],
        schools: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getAllSchool();            // 获取所有学校
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    /*
     * 返回上一页
     */
    returnMainPage() {
        wx.navigateBack({ delta: 1 });
    },
    /*
     * 获取所有省份
     */
    async getAllSchool() {
        school.getAllSchool().then(async (res) => {
            // 渲染所有省份
            let list = res.provinceList;
            for (let i = 0; i < list.length; i++) {
                list[i].selected = false;
            }
            this.setData({
                provinces: list
            });
            // 点击选择第一个院校
            let old_arr = this.data.provinces;
            const clickedIndex = 0;
            // 清除其他所有盒子的class属性
            for (let i = 0; i < old_arr.length; i++) {
                old_arr[i].selected = false;
            }
            // 设置自己的class属性
            old_arr[clickedIndex].selected = true;
            // 获取该省份下的所有学校
            let schoolList = old_arr[clickedIndex].schoolList;
            // 添加院校logoURL
            for (let i = 0; i < schoolList.length; i++) {
                if (schoolList[i].iconUrl) {
                    continue;
                }
                let iconId = schoolList[i].iconId;
                try {
                    let iconUrl = await file.downloadPublicFile(iconId);
                    schoolList[i].iconUrl = iconUrl.data.url;
                } catch (err) {
                    continue;
                }
            }
            console.log(schoolList);
            // 渲染页面
            this.setData({
                provinces: old_arr,
                schools: schoolList
            });
        }).catch((err) => {
            wx.showToast({
                title: '请检查网络连接',
                icon: 'none',
                duration: 1000
            });
        });
    },
    /*
     * 将点击的盒子类设置为 被点击状态
     */
    async setProvinceSelectedClass(e) {
        let old_arr = this.data.provinces;
        const clickedIndex = e.currentTarget.dataset.index;
        // 清除其他所有盒子的class属性
        for (let i = 0; i < old_arr.length; i++) {
            old_arr[i].selected = false;
        }
        // 设置自己的class属性
        old_arr[clickedIndex].selected = true;
        // 获取该省份下的所有学校
        let schoolList = old_arr[clickedIndex].schoolList;
        // 添加院校logoURL
        for (let i = 0; i < schoolList.length; i++) {
            if (schoolList[i].iconUrl) {
                continue;
            }
            let iconId = schoolList[i].iconId;
            try {
                let iconUrl = await file.downloadPublicFile(iconId);
                schoolList[i].iconUrl = iconUrl.data.url;
            } catch (err) {
                continue;
            }
        }
        // 渲染页面
        this.setData({
            provinces: old_arr,
            schools: schoolList
        });
    },
    openSchoolInfo(e) {
        const schoolId = e.currentTarget.dataset.school.id;
        wx.navigateTo({
          url: `/pages/schoolInfo/schoolInfo?schoolId=${schoolId}`,
        });
    }
})